package com.byzk.p2p.admin.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.system.dto.GoodType;
import com.byzk.p2p.admin.system.service.GoodTypeService;

@Controller
@RequestMapping("/admin/goodType")
public class GoodTypeController extends AdminBaseController {

	@Autowired
	private GoodTypeService goodTypeService;

	/**
	 * 分页查询商品类型
	 * @param model
	 * @param info
	 * @return
	 */
	@RequestMapping("/list")
	public String listGoodType(Model model, @ModelAttribute GoodType info) {
		PageInfo<GoodType> pageInfo = goodTypeService.listAll(info);
		model.addAttribute("pageInfo", pageInfo);
		return ftl(Thread.currentThread().getStackTrace());
	}

	/**
	 * 新增或修改商品类型
	 * 
	 * @param good
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/saveOrUpdate")
	public void saveOrUpdate(Model model, @RequestBody GoodType info) throws Exception {
		try {
			goodTypeService.saveOrUpdate(info);
			super.printJSON(new JsonMsg(-1, "新增成功"));
		} catch (Exception e) {
			// TODO: handle exception
			super.printJSON(new JsonMsg(1, "新增失败"));
		}
	}
	
	/**
	 * 删除商品类型
	 * @param model
	 * @param id
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public void delete(Model model, @RequestParam("id") Integer id) throws Exception {
		try {
			Integer count=goodTypeService.getCount(id);
			if(count!=null && count>0){
				super.printJSON(new JsonMsg(2, "商品类型已选用,不可删除!"));
				return;
			}
			goodTypeService.delete(id);
			super.printJSON(new JsonMsg(-1, "删除成功"));
		} catch (Exception e) {
			 throw new Exception(e);
			// TODO: handle exception
		}
	}
	
	/**
	 * 进入修改页
	 * @param model
	 * @param info
	 * @return
	 */
	@RequestMapping("/updateGoodType")
	public String updateGoodType(Model model, @RequestParam("id") Integer id) {
		GoodType type = goodTypeService.selectById(id);
		model.addAttribute("goodType",type);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	/**
	 * 进入新增页面
	 * @param model
	 * @param info
	 * @return
	 */
	@RequestMapping("/addGoodType")
	public String addGoodType() {
		return ftl(Thread.currentThread().getStackTrace());
	}

}
